﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using System.Linq;
using System.Data.Linq;
using System.Threading;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

namespace DevEx
{
    public partial class ucTonDauKy : DevExpress.XtraEditors.XtraUserControl
    {
        QLVTDataContext dc = new QLVTDataContext();
        DataTable dtTon;
        
        public ucTonDauKy()
        {
            InitializeComponent();
        }

        private void ucTonDauKy_Load(object sender, EventArgs e)
        {
            gridView2.OptionsBehavior.Editable = false;
            //set editable = false để bắt event double click
            KhoiTaoKho();
            KhoiTaotlLoaiVatTu();
            KhoiTaodtTon();
            KhoiTaoDMVatTu();            
        }


        public void KhoiTaoKho()
        {
            CKho m_kho = new CKho();
            lueKho.Properties.DisplayMember = "Kho";
            lueKho.Properties.ValueMember = "ID_Kho";

            lueKho.Properties.DataSource = m_kho.SelectAllRecord();
            lueKho.Properties.PopulateColumns();            
            lueKho.Properties.Columns["ID_Kho"].Visible = false;
            
        }
        public void KhoiTaoDMVatTu()
        {
            CVatTu m_VatTu = new CVatTu();
            var q = from p in dc.tblVatTus                     
                    where p.Cuoi == true
                    select new { p.ID_VatTu ,p.VatTu};

            dtgVatTu.DataSource = q;
            gridView2.Columns.ColumnByFieldName("ID_VatTu").Visible = false;           

        }


        public void KhoiTaoTonDauKy(int id_Kho)
        {
            var q = from p in dc.tblTons
                    where p.ID_Kho == id_Kho
                    orderby p.tblVatTu.VatTu, p.Code
                    select new {p.ID_MatHang,
                    p.ID_VatTu,
                    p.tblVatTu.VatTu,
                    p.Code,                    
                    p.ID_CCL,
                    p.tblCCL.CCL,
                    p.Gia,
                    p.NamSX,
                    p.SLTonKH,
                    p.ID_NguonVatTu,
                    p.tblNguonVatTu.NguonVatTu,
                    p.NgayTaoTon,
                    p.SoTheKho,
                    p.TangKeKhoang,
                    p.NguonGoc 
                    };

            //dtgTonDauKy.DataSource = q;
            dtTon.Clear();
            foreach (var ton in q)
            {
                DataRow row = dtTon.NewRow();
                row["ID_MatHang"] = ton.ID_MatHang;
                row["ID_VatTu"] = ton.ID_VatTu;
                row["VatTu"] = ton.VatTu;
                row["Code"] = ton.Code;
                row["NguonGoc"] = ton.NguonGoc;
                row["ID_CCL"] = ton.ID_CCL;
                row["Gia"] = ton.Gia;                
                row["NamSX"] = ton.NamSX ?? "";
                row["SLTonKH"] = ton.SLTonKH;
                row["ID_NguonVatTu"] = ton.ID_NguonVatTu;
                row["NgayTaoTon"] = ton.NgayTaoTon;
                row["SoTheKho"] = ton.SoTheKho;
                row["TangKeKhoang"] = ton.TangKeKhoang;
                row["Status"] = 0;//binh thuong
                dtTon.Rows.Add(row);
            }
            dtgTonDauKy.DataSource = dtTon;
            gridView1.Columns.ColumnByFieldName("ID_VatTu").Visible = false;
            gridView1.Columns.ColumnByFieldName("ID_MatHang").Visible = false;
            gridView1.Columns.ColumnByFieldName("Status").Visible = false;
            gridView1.Columns.ColumnByFieldName("Code").Visible = false;

            //Columns VatTu
            var cl_VatTu = gridView1.Columns.ColumnByFieldName("VatTu");
            cl_VatTu.BestFit();
            cl_VatTu.Caption = "Vật Tư";
            cl_VatTu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_VatTu.OptionsColumn.AllowEdit = false;
            cl_VatTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            
            //Columns CCL
            var cl_CCL = gridView1.Columns.ColumnByFieldName("ID_CCL");
            cl_CCL.Width = 50;
            cl_CCL.Caption = "CCL";
            cl_CCL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_CCL.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns Nguon Nhap
            var cl_NguonVatTu = gridView1.Columns.ColumnByFieldName("ID_NguonVatTu");
            cl_NguonVatTu.Width = 80;
            cl_NguonVatTu.Caption = "Nguồn nhập";
            cl_NguonVatTu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_NguonVatTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //format TCNX
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repositoryItemLookUpEdit_NguonVatTu = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            CNguonVatTu m_nvt = new CNguonVatTu();
            repositoryItemLookUpEdit_NguonVatTu.DataSource = m_nvt.SelectAllRecord();

            repositoryItemLookUpEdit_NguonVatTu.DisplayMember = "NguonVatTu";
            repositoryItemLookUpEdit_NguonVatTu.ValueMember = "ID_NguonVatTu";
            repositoryItemLookUpEdit_NguonVatTu.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo("NguonVatTu", "NguonVatTu", 20, DevExpress.Utils.FormatType.None, "", true, DevExpress.Utils.HorzAlignment.Default, DevExpress.Data.ColumnSortOrder.None)});
            //repositoryItemLookUpEdit_NguonVatTu.Appearance.Font = new Font(this.Appearance.Font.Name, 10);
            cl_NguonVatTu.ColumnEdit = repositoryItemLookUpEdit_NguonVatTu;


            //Columns NgayTaoTon
            var cl_NgayTaoTon = gridView1.Columns.ColumnByFieldName("NgayTaoTon");
            cl_NgayTaoTon.Width = 80;
            cl_NgayTaoTon.Caption = "Ngày TTồn";
            cl_NgayTaoTon.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NgayTaoTon.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns Code
            var cl_Code = gridView1.Columns.ColumnByFieldName("Code");
            cl_Code.Width = 50;
            cl_Code.OptionsColumn.AllowEdit = false;
            //cl_NgayTaoTon.Caption = "Ngày Tạo Tồn";
            cl_Code.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Code.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns NamSX
            var cl_NamSX = gridView1.Columns.ColumnByFieldName("NamSX");
            cl_NamSX.Width = 60;
            cl_NamSX.Caption = "Năm SX";
            cl_NamSX.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NamSX.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns NguonGoc
            var cl_NguonGoc = gridView1.Columns.ColumnByFieldName("NguonGoc");            
            cl_NguonGoc.Caption = "Mã Hiệu";
            cl_NguonGoc.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_NguonGoc.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns SoTheKho
            var cl_SoTheKho = gridView1.Columns.ColumnByFieldName("SoTheKho");
            cl_SoTheKho.Width = 60;
            cl_SoTheKho.Caption = "Thẻ Kho";
            cl_SoTheKho.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_SoTheKho.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns TangKeKhoang
            var cl_TangKeKhoang = gridView1.Columns.ColumnByFieldName("TangKeKhoang");
            cl_TangKeKhoang.Width = 80;
            cl_TangKeKhoang.Caption = "Vị trí";
            cl_TangKeKhoang.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_TangKeKhoang.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns Gia
            var cl_Gia = gridView1.Columns.ColumnByFieldName("Gia");
            cl_Gia.Width = 80;
            cl_Gia.Caption = "Đơn giá";
            cl_Gia.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_Gia.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Gia.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_Gia.DisplayFormat.FormatString = "n0";

            
            //Columns SLTonKH
            var cl_SLTonKH = gridView1.Columns.ColumnByFieldName("SLTonKH");
            cl_SLTonKH.Width = 80;
            cl_SLTonKH.Caption = "SL Tồn";
            cl_SLTonKH.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_SLTonKH.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_SLTonKH.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_SLTonKH.DisplayFormat.FormatString = "n0";

            
            

                       
            //repositoryItemLookUpEdit1.AutoHeight = false;
            //repositoryItemLookUpEdit1.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            //new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            //repositoryItemLookUpEdit1.DropDownRows = 10;
            //repositoryItemLookUpEdit1.Name = "repositoryItemLookUpEdit1";
            //repositoryItemLookUpEdit1.PopupWidth = 220;



            

            //format CCL
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repositoryItemLookUpEdit_CCL = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            CCCL m_ccl = new CCCL();
            repositoryItemLookUpEdit_CCL.DataSource = m_ccl.SelectAllRecord();

            repositoryItemLookUpEdit_CCL.DisplayMember = "CCL";
            repositoryItemLookUpEdit_CCL.ValueMember = "ID_CCL";
            repositoryItemLookUpEdit_CCL.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo("CCL", "CCL", 20, DevExpress.Utils.FormatType.None, "", true, DevExpress.Utils.HorzAlignment.Default, DevExpress.Data.ColumnSortOrder.None)});
            ((DevExpress.XtraGrid.Views.Base.ColumnView)dtgTonDauKy.MainView).Columns["ID_CCL"].ColumnEdit = repositoryItemLookUpEdit_CCL;

        }

        public void KhoiTaodtTon()
        {
            dtTon = new DataTable();
            dtTon.Columns.Add("ID_MatHang", typeof(int));
            dtTon.Columns.Add("ID_VatTu", typeof(int));
            dtTon.Columns.Add("VatTu", typeof(String));
            dtTon.Columns.Add("Code", typeof(String));
            dtTon.Columns.Add("NguonGoc", typeof(String));
            dtTon.Columns.Add("NamSX", typeof(int));
            dtTon.Columns.Add("ID_CCL", typeof(int));
            dtTon.Columns.Add("Gia", typeof(Double));            
            dtTon.Columns.Add("SLTonKH", typeof(Double));
            dtTon.Columns.Add("ID_NguonVatTu", typeof(int));
            dtTon.Columns.Add("NgayTaoTon", typeof(DateTime));
            dtTon.Columns.Add("SoTheKho", typeof(String));
            dtTon.Columns.Add("TangKeKhoang", typeof(String));
            dtTon.Columns.Add("Status", typeof(int));//0: binh thuong, 1: them, 2: sua, 3: xoa
        }
            
        private void btnThem_Click(object sender, EventArgs e)
        {
            DateTime NgayTaoTon;
            NgayTaoTon = DateTime.Today;
            CreatNewRowTon(Convert.ToInt32(gridView2.GetFocusedRowCellValue("ID_VatTu")), 
                Convert.ToString(gridView2.GetFocusedRowCellValue("VatTu")), NgayTaoTon);
        }

        private void CreatNewRowTon(int ID_VatTu, String VatTu, DateTime NgayTaoTon)
        {
            gridView1.AddNewRow();

            int rowHandle = gridView1.GetRowHandle(gridView1.DataRowCount);
            if (gridView1.IsNewItemRow(rowHandle))
            {
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ID_VatTu"], ID_VatTu);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["VatTu"], VatTu);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["NgayTaoTon"], NgayTaoTon);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ID_CCL"], 1);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["Status"], 1);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["Gia"], 0);                
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["NguonGoc"], "");
            }
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            CTon m_ton = new CTon();

            for (int i = 0; i < gridView1.RowCount; i++)
            {

                int status = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["Status"]));
                if (status==1)
                { //Thêm
                    tblTon ton = new tblTon();
                    ton.ID_VatTu = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_VatTu"]));
                    ton.NguonGoc = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["NguonGoc"]));
                    ton.ID_Kho = Convert.ToInt32(lueKho.EditValue);
                    ton.ID_CCL = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_CCL"]));                    
                    ton.ID_NguonVatTu = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_NguonVatTu"]));
                    ton.NamSX = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["NamSX"]));
                    ton.Gia = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["Gia"]));                    
                    // gia tri Ton ban dau giong nhau = SLTonKH
                    ton.SLTonKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLTonKH"]));
                    ton.SLTonTH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLTonKH"]));
                    ton.SLTonSS = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLTonKH"]));
                    ton.SLSS11 = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLTonKH"]));
                    ton.NgayTaoTon = Convert.ToDateTime(gridView1.GetRowCellValue(i, gridView1.Columns["NgayTaoTon"]));
                    ton.SoTheKho = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["SoTheKho"]));
                    ton.TangKeKhoang = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["TangKeKhoang"]));

                    m_ton.InsertTonDauKy(ton);

                }
                //Xóa
                if (status==3)
                {
                    //kiểm tra mặt hàng có đang được sử dụng không
                    
                    int ID_MatHang = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_MatHang"]));
                    int count_MatHang = (from p in dc.tblSLNXes
                                         where p.ID_MatHang == ID_MatHang
                                         select p).Count();
                    if (count_MatHang > 0)
                    {
                        MessageBox.Show("Mặt hàng đang được sử dụng không xóa được");
                        return;
                    }
                    

                    m_ton.Delete(ID_MatHang);
                }
                //Sửa
                 if (status==2)
                {
                    tblTon ton = new tblTon();
                    ton.ID_MatHang = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_MatHang"]));
                    ton.ID_VatTu = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_VatTu"]));
                    ton.NguonGoc = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["NguonGoc"]));
                    ton.Code = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["Code"]));
                    ton.ID_Kho = Convert.ToInt32(lueKho.EditValue);
                    ton.ID_CCL = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_CCL"]));
                    ton.ID_NguonVatTu = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_NguonVatTu"]));
                    ton.NamSX = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["NamSX"]));
                    ton.Gia = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["Gia"]));
                    
                    // gia tri Ton ban dau giong nhau = SLTonKH
                    ton.SLTonKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLTonKH"]));
                    ton.SLTonTH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLTonKH"]));
                    ton.SLTonSS = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLTonKH"]));
                    ton.NgayTaoTon = Convert.ToDateTime(gridView1.GetRowCellValue(i, gridView1.Columns["NgayTaoTon"]));
                    ton.SoTheKho = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["SoTheKho"]));
                    ton.TangKeKhoang = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["TangKeKhoang"]));
                    m_ton.Update(ton);
                }
            }

            KhoiTaoTonDauKy(Convert.ToInt32(lueKho.EditValue));            
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            KhoiTaoTonDauKy(Convert.ToInt32(lueKho.EditValue));            
        }

        private void lueKho_EditValueChanged(object sender, EventArgs e)
        {
            KhoiTaoTonDauKy(Convert.ToInt32(lueKho.EditValue));            
        }

       
        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                int Status = Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, gridView1.Columns["Status"]));
                if (Status == 1)//add new
                {
                    //e.Appearance.BackColor = Color.Blue;
                    //e.Appearance.BackColor2 = Color.SeaShell;
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                }
                if (Status == 2)//update row
                {
                    //e.Appearance.BackColor = Color.MediumSpringGreen;
                    //e.Appearance.BackColor2 = Color.SeaShell;
                    e.Appearance.ForeColor = Color.Blue;
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Italic);
                }
                if (Status == 3)//delete row
                {
                    //e.Appearance.BackColor = Color.Yellow;
                    //e.Appearance.BackColor2 = Color.SeaShell;
                    e.Appearance.ForeColor = Color.Red;
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Strikeout);
                }
            }
        }

        CVatTu m_vatTu = new CVatTu();

        public void KhoiTaotlLoaiVatTu()
        {
            tlLoaiVatTu.BeginUpdate();
            tlLoaiVatTu.Columns.Add();
            tlLoaiVatTu.Columns[0].Caption = "ID_VatTu";
            tlLoaiVatTu.Columns[0].VisibleIndex = 0;
            tlLoaiVatTu.Columns[0].Visible = false;
            tlLoaiVatTu.Columns.Add();
            tlLoaiVatTu.Columns[1].Caption = "Ma_VatTu";
            tlLoaiVatTu.Columns[1].VisibleIndex = 1;
            tlLoaiVatTu.Columns[1].Visible = false;
            tlLoaiVatTu.Columns.Add();
            tlLoaiVatTu.Columns[2].Caption = "VatTu";
            tlLoaiVatTu.Columns[2].VisibleIndex = 2;
            tlLoaiVatTu.Columns.Add();
            tlLoaiVatTu.Columns[3].Caption = "Ma_LoaiVatTu";
            tlLoaiVatTu.Columns[3].VisibleIndex = 3;
            tlLoaiVatTu.Columns[3].Visible = false;
            tlLoaiVatTu.Columns.Add();
            tlLoaiVatTu.Columns[4].Caption = "ID_DVT";
            tlLoaiVatTu.Columns[4].VisibleIndex = 4;
            tlLoaiVatTu.Columns[4].Visible = false;
            tlLoaiVatTu.Columns.Add();
            tlLoaiVatTu.Columns[5].Caption = "Cuoi";
            tlLoaiVatTu.Columns[5].VisibleIndex = 5;
            tlLoaiVatTu.Columns[5].Visible = false;
            tlLoaiVatTu.EndUpdate();
            tlLoaiVatTu.OptionsSelection.MultiSelect = false;

            tlLoaiVatTu.LookAndFeel.UseDefaultLookAndFeel = false;
            tlLoaiVatTu.LookAndFeel.SkinName = "iMaginary";
            tlLoaiVatTu.OptionsBehavior.Editable = false;
            //tlLoaiVatTu.OptionsView.ShowColumns = false;
            tlLoaiVatTu.OptionsView.ShowIndicator = false;
            tlLoaiVatTu.OptionsView.ShowHorzLines = false;
            tlLoaiVatTu.OptionsView.ShowVertLines = false;


            //Tạo nút gốc
            TreeListNode root = null;
            var vatTu = m_vatTu.SelectRecordByID(1);

            TreeListNode newNode;
            newNode = tlLoaiVatTu.AppendNode(new object[] { vatTu.ID_VatTu, vatTu.Ma_VatTu, vatTu.VatTu, vatTu.Ma_LoaiVatTu, vatTu.ID_DVT }, root);

            //tlLoaiVatTu.EndUnboundLoad();
            KhoiTaotreelistLoaiVatTu(Convert.ToString(newNode.GetValue("Ma_VatTu")), newNode);
        }

        #region "event treelistLoaiVatTu"
        public void KhoiTaotreelistLoaiVatTu(String LoaiVatTu, TreeListNode parentNode)
        {
            var listVatTu = m_vatTu.SelectAllRecordByLoaiVatTu_Cuoi(LoaiVatTu, false);

            TreeListNode newNode;
            foreach (tblVatTu vatTu in listVatTu)
            {
                newNode = tlLoaiVatTu.AppendNode(new object[] { vatTu.ID_VatTu, vatTu.Ma_VatTu, vatTu.VatTu, vatTu.Ma_LoaiVatTu, vatTu.ID_DVT }, parentNode);
            }
        }

        private void tlLoaiVatTu_AfterFocusNode(object sender, NodeEventArgs e)
        {
            //MessageBox.Show(e.Node.GetValue("Ma_VatTu").ToString());
            String maVatTu = Convert.ToString(tlLoaiVatTu.FocusedNode.GetValue("Ma_VatTu"));

            if (!e.Node.HasChildren)
            {
                KhoiTaotreelistLoaiVatTu(maVatTu, e.Node);
            }
            SetEditFromTree();

            //Loc vat tu

        }

        private void SetEditFromTree()
        {
            if (tlLoaiVatTu.FocusedNode == null)
                return;

            PopupContainerEdit editor = GetParentEditor(tlLoaiVatTu);
            if (editor == null)
                return;
            editor.EditValue = tlLoaiVatTu.FocusedNode.GetValue("VatTu") ?? "";

        }

        static PopupContainerControl GetParentContainer(Control ctrl)
        {
            if (ctrl == null)
                return null;
            return ctrl.Parent as PopupContainerControl;
        }

        static PopupContainerEdit GetParentEditor(Control ctrl)
        {
            PopupContainerControl container = GetParentContainer(ctrl);
            return container == null ? null : container.OwnerEdit;
        }

        private void tlLoaiVatTu_DoubleClick(object sender, EventArgs e)
        {
            SetEditFromTree();

            // close the popup
            PopupContainerEdit editor = GetParentEditor(sender as Control);
            if (editor != null)
                editor.ClosePopup();
        }
        #endregion
        

        private void dtgVatTu_DoubleClick(object sender, EventArgs e)
        {
            //dtgDMVatTu
            btnThem_Click(sender, e);
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName != "Status")
            {
                try
                {
                    if (Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Status"])) != 1)
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Status"], 2);
                }
                catch { }// bo qua loi them moi
                
            }            
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Status"])) == 1)
                gridView1.DeleteRow(gridView1.FocusedRowHandle);
            else
                gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Status"], 3);            
        }

        
        

            
    }
}

